package com.my.helper.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.my.helper.R;
import com.my.helper.adapter.AiAdapter;
import com.my.helper.bean.AiData;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class ChatActivity extends AppCompatActivity {

    public static void startActivity(Context activity) {
        Intent intent = new Intent(activity, ChatActivity.class);
        activity.startActivity(intent);
    }

    private RecyclerView recyclerView;

    private AiAdapter mAdapter;

    private EditText inputEt;

    private TextView sendBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        initViews();
        initAdapter();
    }

    private void initViews() {
        recyclerView = findViewById(R.id.recyclerView);
        inputEt = findViewById(R.id.etInput);
        sendBtn = findViewById(R.id.sendInput);
    }

    private void initAdapter() {
        mAdapter = new AiAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(inputEt.getText())) {
                    Toast.makeText(ChatActivity.this, "请输入发送的内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                askData(inputEt.getText().toString());
            }
        });
    }

    private void askData(String text) {
        if (TextUtils.isEmpty(text)) return;
        if (text.equals("？") || text.equals("。")) {
            return;
        }
        AiData aiData = new AiData();
        aiData.item_type = 1;
        aiData.setReply(text);
        mAdapter.addAiData(aiData, recyclerView);
        sendMsg(inputEt.getText().toString());
        inputEt.setText("");
    }

    private void sendMsg(String msg) {
        OkHttpUtils
                .get()
                .url("http://i.itpk.cn/api.php?question=" + msg)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        AiData aiData = new AiData();
                        aiData.setReply(response);
                        mAdapter.addAiData(aiData, recyclerView);
                    }
                });
    }
}
